package client;

import java.util.Collection;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import test.TestFacadeRemote;
import ema.tpjee.banking.dao.AccountDao;
import ema.tpjee.banking.dao.CustomerDao;
import ema.tpjee.banking.model.Account;
import ema.tpjee.banking.model.Bank;
import ema.tpjee.banking.model.City;
import ema.tpjee.banking.model.Customer;

public class TestBank {


	private TestFacadeRemote facadeBean;
	private AccountDao accountDao;
	private CustomerDao customerDao;
	
	
	
	public static void main(String[] args){
		TestBank tb = new TestBank();
		tb.createDao();
		try {
			tb.test();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private void testAccount(Customer c0) {
		
		Account acnt0 = accountDao.createAccount(c0 ,100, 10);
		
		System.out.println("account created " + acnt0.getAccountNumber());
		
		Account acnt1 = accountDao.createAccount(c0, 0, 0);
		System.out.println("account created " + acnt1.getAccountNumber());

		accountDao.creditAccount(acnt0, 1000);
		accountDao.creditAccount(acnt1, 10000);
		System.out.println("balance of " + acnt0.getAccountNumber() + " is "
				+ acnt0.getBalance());
		System.out.println("balance of " + acnt1.getAccountNumber() + " is "
				+ acnt1.getBalance());
		double totransfer = 500;
		System.out.println("transfer of " + totransfer + " from "
				+ acnt0.getAccountNumber() + " to " + acnt1.getAccountNumber());
		try {
			accountDao.transferAccount(acnt0, acnt1, totransfer);
			System.out.println("balance of " + acnt0.getAccountNumber()
					+ " is " + acnt0.getBalance());
			System.out.println("balance of " + acnt1.getAccountNumber()
					+ " is " + acnt1.getBalance());

		} catch (Exception e) {
			System.out.println("rolled back transfer of " + totransfer + " from "
					+ acnt0.getAccountNumber() + " to "
					+ acnt1.getAccountNumber() + " "+ e.getMessage());
		}
	}
	
	public void test() throws Exception {
		// Initialisation de la base
		facadeBean.populate();
		
	    System.out.println("Liste des communes");
		
		List<City> cities = facadeBean.getCities();
		if (cities!=null){
			for (City city : cities) {
				System.out.println(city);
			}
		}
		
		City thesecond = facadeBean.getCities().get(1);
		System.out.print(thesecond);
		facadeBean.delete(thesecond);
		
		//facadeBean.delete(facadeBean.getCities().get(1));
		
		System.out.println("Liste des communes après la suppression de la seconde");
		
		cities = facadeBean.getCities();
		if (cities!=null){
			for (City city : cities) {
				System.out.println(city);
			}
		}		
	
		List<Bank> banks = facadeBean.getBanks();
		if (banks != null) {
			for (Bank bank : banks) {
				System.out.println("Liste des clients pour la banque "
						+ bank.getName() + " :");
				//Collection<Customer> customers = bank.getCustomers();
				
				Collection<Customer> customers = customerDao.getList(bank);
				
				if (customers == null) {
					System.out.println(" Aucun client");
				} else {
					for (Customer customer : customers){
						System.out.println("    client: " + customer);
						//List<Account> cnts=customer.getAccounts();
						
						
						List<Account> cnts= accountDao.getList(customer);
						
						
						int siz = cnts.size();
						for (Account account : cnts) {
							System.out.println("        account: " + account.getAccountNumber());
						}
					}
				}
			}
		} else {
			System.out.println("Aucune banque");
		}
		// Recuperation de la liste des Customers
		System.out.println("");
		System.out.println("Liste des clients + banque :");
		List<Customer> customers = facadeBean.getCustomers();
		if (customers != null) {
			facadeBean.getCustomers().get(0).setCity(facadeBean.getCities().get(0));
			facadeBean.save(facadeBean.getCustomers().get(0));
			for (Customer customer : customers) {
				System.out.println("client: " + customer.toString()
						+ " /banque: " + customer.getBank().getName());
			}
		}

		testAccount();
		
		//Customer c0 = facadeBean.getCustomers().get(0);
		//testAccount(c0);
		
		
		// suppression d'un client
		
		try {
			Customer del = facadeBean.getCustomers().get(1);
			System.out.println("suppression du client " + del);
			facadeBean.delete(del);
		} catch (Exception e) {
			System.out.println("erreur pendant la suppression du client");
		}
	
		
		System.out.println("Liste des clients après la suppression");
		customers = facadeBean.getCustomers();
		if (customers != null) {
			for (Customer Customer : customers) {
				System.out.println(Customer.getName());
			}
		}
		
		// suppression d'une banque

		Bank toDelete = facadeBean.getBanks().get(1);
		System.out.println("suppression de la banque " + toDelete);
		facadeBean.delete(toDelete);

		System.out.println("Liste des banques après la suppression");
		banks = facadeBean.getBanks();
		if (banks != null) {
			for (Bank bank : banks)
				System.out.println(bank.getName());

		}
		
		
		System.out.println("Liste des clients après la suppression de la banque");
		customers = facadeBean.getCustomers();
		if (customers != null) {
			for (Customer Customer : customers) {
				System.out.println(Customer.getName());
			}
		}
		
	
	
	}

	private void testAccount() {
		Customer c0 = facadeBean.getCustomers().get(0);
		accountDao.testAccount(c0);	
	}


	public void createDao() {
		System.out.println("creating  EjbDao");
		System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		System.setProperty(Context.PROVIDER_URL, "localhost:1099");
		System.setProperty(Context.URL_PKG_PREFIXES,
				"org.jboss.naming:org.jnp.interfaces");
		try {
			facadeBean = (TestFacadeRemote) new InitialContext()
	        .lookup("TestFacade/remote");			
			accountDao = (AccountDao) new InitialContext()
	        .lookup("AccountDaoImpl/remote");	
			customerDao = (CustomerDao) new InitialContext()
	        .lookup("CustomerDaoImpl/remote");	
			
			
				
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	



}
